package com.barneyx.mshinfoplatform.web.front.info;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.barneyx.mshinfoplatform.common.ExResult;
import com.barneyx.mshinfoplatform.entity.enums.EInfoState;
import com.barneyx.mshinfoplatform.entity.info.article.ArticleEntity;
import com.barneyx.mshinfoplatform.exception.MshException;
import com.barneyx.mshinfoplatform.util.AreaUtil;
import com.barneyx.mshinfoplatform.util.ArticleUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;

@RestController
@RequestMapping("/api/front/article/")
public class ArticleController {
    @Resource
    private ArticleUtil articleUtil;

    @Resource
    public AreaUtil areaUtil;


    @GetMapping("detail")
    public ExResult getDetail(@RequestParam("id") String articleId) {
        return ExResult.ofAuto(articleUtil.getDetail(articleId));
    }

    @GetMapping("news-page")
    public ExResult<?> page(@RequestParam(value = "cateid", required = false) String cateId,
                            @RequestParam(value = "current", defaultValue = "1", required = false) long current,
                            @RequestParam(value = "size", defaultValue = "20", required = false) long size) throws MshException {
        Page<ArticleEntity> page = articleUtil.getPage(current, size, new LambdaQueryWrapper<ArticleEntity>()
                .func(StrUtil.isBlank(cateId), a -> {
                    try {
                        a.allEq(MapUtil.builder(new HashMap<SFunction<ArticleEntity, ?>, Object>())
                                        .put(ArticleEntity::getState, EInfoState.PASS)
                                        .put(ArticleEntity::getAreaId, areaUtil.getCurrentAreaId())
                                        .build())

                                .orderByDesc(ArticleEntity::getUpdateAt);
                    } catch (MshException e) {
                        throw new RuntimeException(e);
                    }
                })
                .func( StrUtil.isNotBlank(cateId), a ->
                {
                    try {
                        a.allEq(MapUtil.builder(new HashMap<SFunction<ArticleEntity, ?>, Object>())
                                        .put(ArticleEntity::getState, EInfoState.PASS)
                                        .put(ArticleEntity::getAreaId, areaUtil.getCurrentAreaId())
                                        .put(ArticleEntity::getCarticleCateId, cateId)
                                        .build())
                                .orderByDesc(ArticleEntity::getCreateAt);
                    } catch (MshException e) {
                        throw new RuntimeException(e);
                    }
                })
                );
        return ExResult.ofAuto(page);
    }


    @PostMapping("create")
    public ExResult<?> createArticle(@RequestBody ArticleEntity article) {
        return ExResult.ofAuto(articleUtil.add(article), article);
    }

    @GetMapping("inc-click")
    public ExResult<?> incClick(@RequestParam("id") String articleId) {
        return ExResult.ofAuto(articleUtil.updateClick(articleId));
    }

    @PutMapping("update")
    public ExResult<?> updateArticle(@RequestBody ArticleEntity article) {
        return ExResult.ofAuto(articleUtil.updateAll(article), article);
    }

}
